'''
Author: duliang thinktanker@163.com
Date: 2024-06-25 23:09:28
LastEditors: duliang thinktanker@163.com
LastEditTime: 2024-07-06 19:01:18
FilePath: 
Description: 
'''
import MYQQ_http
from flask import Flask, request, jsonify
from urllib.parse import unquote
import time
from datetime import datetime
from threading import Thread
import uuid
import requests
from getsw import getsw


def get_uuid():
    return str(uuid.getnode())


HOUR = 19
MIN = 2
SEC = 0
QQNUM = 3858582700
QQGROUP = 980964246
APIport = 8848
token = 1101
callback_port = 5004
MYQQ_http.echo_mode = True
MYQQ_http.global_set(APIport, token, callback_port)
QQ = MYQQ_http.robot(QQNUM)
SWURL = 'https://16gs832674.oicp.vip/qq'


def check_time():
    content = ''
    while 1:
        now = datetime.now()
        print(now, end='\r')
        if (now.hour == HOUR) and (now.minute == MIN) and (now.second == SEC):
            content = getsw()
            # 获取水情信息发送到群里 content = get_water_info()
            MYQQ_http.send_msg(bot_name=QQ,
                               toQQ=QQGROUP,
                               content=content,
                               anony=False,
                               group=True)
        time.sleep(1)


# MYQQ_http.send_msg(bot_name=QQ,
#                    toQQ=QQGROUP,
#                    content=str(int(time.time())),
#                    anony=False,
#                    group=True)
app = Flask(__name__)


@app.route('/qq', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        print(request.json)
        resp_json = request.json
        try:
            t = resp_json['MQ_type']
            msg = unquote(resp_json['MQ_msg'], 'utf-8')
            print(msg)
        except Exception as e:
            print(e)

        return jsonify({'status': 'ok'})
    else:
        return jsonify({'status': 'ok'})


if __name__ == '__main__':
    t = Thread(target=check_time)
    t.daemon = True
    t.start()
    app.run(host='0.0.0.0', port=5005)
